const defaultState={
    backUserInfo:JSON.parse(window.localStorage.getItem('backUserInfo'))||{},//后台用户信息
    backToken:window.localStorage.getItem('backToken')||"",
    curList:[], //当前商品渲染列表
    sallingTotal:null, //1
    stokeTotal:null,//2
    salledTotal:null,//3
    recycleTotal:null,//4
    classifyList:[],//商品分类
    levelList:[],//分类层次表
    formatList:[],//商品规格列表
    evaluatList:[],//评论规格列表
    newShop:{},//添加新商品
    newClassity:{},//添加新分类
    getPinShop: [],// 拼团商品数据
    opreaterList:[]
}

const reducer = (state = defaultState, action) => {
    const { type, payload } = action;
    // console.log(payload,'reducer-payload')
    switch (type) {
        case 'GET_LOGIN':
            return {
                ...state,
                backUserInfo: { ...payload }
            }
        case 'GET_OPREATERS':
            return {
                ...state,
                opreaterList:payload
            }
        case 'GET_PINSHOP':
            return {
                ...state,
                getPinShop: payload,
            }
        case 'GET_PINSEARCH':
            return {
                ...state,
                getPinShop: payload
            }
        case 'GET_PINPINZ':
            return {
                ...state,
                getPinShop: payload
            }
        case 'GET_SHOP':
            return {
                ...state,
                curList: payload.data,
                sallingTotal: payload.sallingTotal, //1
                stokeTotal: payload.stokeTotal,//2
                salledTotal: payload.salledTotal,//3
                recycleTotal: payload.recycleTotal,//4
            };
        case 'GET_CLASSIFY':

        function getClassify(arr){ 
            let newArr = []
            newArr = arr.filter(item => item.level === 1)
            newArr = newArr.map(item => {
                return {
                    ...item,
                    children: arr.filter(val => val.parentcode === item.code)
                }
            })
            newArr = newArr.map(item => {
                return {
                    ...item,
                    children: item.children.map(val => {
                        return {
                            ...val,
                            children: arr.filter(v => v.parentcode === val.code)
                        }
                    })
                }
            })
            return newArr
            
        }
        function getLevel(arr){ 
            let newArr = []
            newArr = arr.filter(item => item.level === 1)
            newArr = newArr.map(item => {
                let children=[]
                arr.forEach(v => {
                    if (v.parentcode === item.code) {
                        children.push({ ...v,id: v.id, value: v.title, label: v.title,code:v.code })
                    }
                })
                return {
                    value: item.title,
                    label: item.title,
                    code: item.code,
                    parentcode:item.parentcode,
                    id: item.id,
                    children
                }
            })
            console.log(newArr,'level-arr1')
            newArr = newArr.map(item => {
                return {
                    ...item,
                    children: item.children.map(val => {
                        let children=[]
                        arr.forEach(v => {
                            if (v.parentcode === val.code) {
                                // console.log(v)
                                children.push({ ...v,id: v.id, value: v.title, label: v.title,code:v.code })
                            }
                        })
                        return {
                            ...val,
                            children
                        }
                    })
                }
            })
            console.log(newArr,'level-arr2')
            return newArr
            
        }

            const result=getClassify(payload.data)
            const res=getLevel(payload.data)
            return {
                ...state,
                classifyList: result,
                levelList: res
            }
        case 'GET_FORMAT':
            return {
                ...state,
                formatList: payload.data
            }
        case 'GET_EVALUAT':
            return {
                ...state,
                evaluatList:payload.data
            }
        case 'GET_NEWSHOP':
            return {
                ...state,
                newShop:{...state.newShop,...payload}
            }
        case 'GET_NEWCLASSIFY':
            return {
                ...state,
                newClassity:{...state.newClassity,...payload}
            }
        default :
            return { ...state }
    }
}

export default reducer